package cn.com.fuhuitong.main.cart.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import cn.com.fuhuitong.app.BaseApplication
import cn.com.fuhuitong.base.BaseViewModel
import cn.com.fuhuitong.http.BaseResponse
import cn.com.fuhuitong.http.HttpResponse
import cn.com.fuhuitong.http.ViewModelSubscribeListener
import cn.com.fuhuitong.main.cart.entity.CartListResponse
import cn.com.fuhuitong.main.cart.mode.CartManager
import io.reactivex.disposables.Disposable

class CartViewModel(application: Application) : BaseViewModel(application) {

    private val cartManager: CartManager = CartManager()

    val cartListDataLiveData = MutableLiveData<HttpResponse<CartListResponse>>()

    val cartUpdateNumDataLiveData = MutableLiveData<HttpResponse<BaseResponse>>()

    val cartDeleteDataLiveData = MutableLiveData<HttpResponse<BaseResponse>>()

    /**
     * 获取购物车列表
     */
    fun cartList() {
        val listener =
            object : ViewModelSubscribeListener<CartListResponse>(cartListDataLiveData) {}
        val loginDisposable: Disposable =
            cartManager.cartList(BaseApplication.userToken(), listener)
        dispose.add(loginDisposable)
    }

    /**
     * 更新购物车数据
     * */
    fun cartUpdateNum(shopCartId: String, num: String) {
        val listener =
            object : ViewModelSubscribeListener<BaseResponse>(cartUpdateNumDataLiveData) {}
        val loginDisposable: Disposable =
            cartManager.cartUpdateNum(BaseApplication.userToken(), shopCartId, num, listener)
        dispose.add(loginDisposable)
    }

    /**
     * 删除购物车数据
     * */
    fun cartDelete(shopCartIds:List<String>) {
        val listener =
            object : ViewModelSubscribeListener<BaseResponse>(cartDeleteDataLiveData) {}
        val loginDisposable: Disposable =
            cartManager.cartDelete(BaseApplication.userToken(), shopCartIds.toList(), listener)
        dispose.add(loginDisposable)
    }

}